/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedoit.gui;

import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.JRadioButton;
import org.freedoit.FreeDoitConfig;
import org.freedoit.util.Constants;

/**
 *
 * @author wanglei
 */
public class SettingDialog extends javax.swing.JDialog {

    /**
     * Creates new form SettingDialog
     */
    public SettingDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initSetting();
    }
    
    private static AbstractButton findButtonByActionCommand(
            javax.swing.ButtonGroup bg,
            String strActionCommand, 
            AbstractButton defaultButton) {
        AbstractButton buttonSelected = null;
        Enumeration<AbstractButton> em = bg.getElements();
        while (em.hasMoreElements()) {
            buttonSelected = em.nextElement();
            if (buttonSelected.getActionCommand().equals(strActionCommand)) {
                break;
            }
            
            buttonSelected = null;
        }
        
        if (buttonSelected == null) {
            buttonSelected = defaultButton;
        }
        return buttonSelected;
    }
    
    private void initSetting() {
        /* Language setting */
        findButtonByActionCommand(this.bgLang, FreeDoitConfig.getLocale(),
                this.jRadioButtonEn).setSelected(true);
        
        /* Synchronize Frequency setting */
        findButtonByActionCommand(this.bgSyncFreq, 
                String.valueOf(FreeDoitConfig.getSyncFreq()),
                this.jRadioButton5Min).setSelected(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelLang = new javax.swing.JLabel();
        jRadioButtonEn = new javax.swing.JRadioButton();
        jRadioButtonChs = new javax.swing.JRadioButton();
        jRadioButtonCht = new javax.swing.JRadioButton();
        jLabelSyncFreq = new javax.swing.JLabel();
        jRadioButton5Min = new javax.swing.JRadioButton();
        jRadioButton30Min = new javax.swing.JRadioButton();
        jRadioButton1Hour = new javax.swing.JRadioButton();
        jRadioButton2Hour = new javax.swing.JRadioButton();
        jRadioButtonNeverAutoSync = new javax.swing.JRadioButton();
        jButtonOK = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(FreeDoitConfig.getText("dialog.setting.title"));
        setFont(FreeDoitConfig.getFont(0, 12));

        jLabelLang.setFont(FreeDoitConfig.getFont(0, 12));
        jLabelLang.setText(FreeDoitConfig.getText("dialog.setting.label.language"));

        bgLang.add(jRadioButtonEn);
        jRadioButtonEn.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButtonEn.setText(FreeDoitConfig.getText("dialog.setting.label.english"));
        jRadioButtonEn.setActionCommand(Constants.LANG_EN);

        bgLang.add(jRadioButtonChs);
        jRadioButtonChs.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButtonChs.setText(FreeDoitConfig.getText("dialog.setting.label.chinese.simplified"));
        jRadioButtonChs.setActionCommand(Constants.LANG_CHS);

        bgLang.add(jRadioButtonCht);
        jRadioButtonCht.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButtonCht.setText(FreeDoitConfig.getText("dialog.setting.label.chinese.tranditonal"));
        jRadioButtonCht.setActionCommand(Constants.LANG_CHT);

        jLabelSyncFreq.setFont(FreeDoitConfig.getFont(0, 12));
        jLabelSyncFreq.setText(FreeDoitConfig.getText("dialog.setting.label.auto.synchronize.frequency"));

        bgSyncFreq.add(jRadioButton5Min);
        jRadioButton5Min.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButton5Min.setText(FreeDoitConfig.getText("dialog.setting.label.every.5.minutes"));
        jRadioButton5Min.setActionCommand(String.valueOf(Constants.SYNC_FREQ_5_MIN));

        bgSyncFreq.add(jRadioButton30Min);
        jRadioButton30Min.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButton30Min.setText(FreeDoitConfig.getText("dialog.setting.label.every.30.minutes"));
        jRadioButton30Min.setActionCommand(String.valueOf(Constants.SYNC_FREQ_30_MIN));

        bgSyncFreq.add(jRadioButton1Hour);
        jRadioButton1Hour.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButton1Hour.setText(FreeDoitConfig.getText("dialog.setting.label.every.1.hour"));
        jRadioButton1Hour.setActionCommand(String.valueOf(Constants.SYNC_FREQ_1_HOUR));

        bgSyncFreq.add(jRadioButton2Hour);
        jRadioButton2Hour.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButton2Hour.setText(FreeDoitConfig.getText("dialog.setting.label.every.2.hour"));
        jRadioButton2Hour.setActionCommand(String.valueOf(Constants.SYNC_FREQ_2_HOUR));

        bgSyncFreq.add(jRadioButtonNeverAutoSync);
        jRadioButtonNeverAutoSync.setFont(FreeDoitConfig.getFont(0, 12));
        jRadioButtonNeverAutoSync.setText(FreeDoitConfig.getText("dialog.setting.label.never.auto.sync"));
        jRadioButtonNeverAutoSync.setActionCommand(String.valueOf(Constants.SYNC_FREQ_NEVER));

        jButtonOK.setFont(FreeDoitConfig.getFont(0, 12));
        jButtonOK.setText(FreeDoitConfig.getText("button.ok"));
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        Cancel.setFont(FreeDoitConfig.getFont(0, 12));
        Cancel.setText(FreeDoitConfig.getText("button.cancel"));
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jButtonOK)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Cancel)
                        .addGap(92, 92, 92))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelSyncFreq)
                            .addComponent(jLabelLang)
                            .addComponent(jRadioButton5Min)
                            .addComponent(jRadioButton30Min)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButtonEn)
                                .addGap(37, 37, 37)
                                .addComponent(jRadioButtonChs)
                                .addGap(37, 37, 37)
                                .addComponent(jRadioButtonCht)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButtonNeverAutoSync)
                            .addComponent(jRadioButton1Hour)
                            .addComponent(jRadioButton2Hour))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelLang)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonEn)
                    .addComponent(jRadioButtonChs)
                    .addComponent(jRadioButtonCht))
                .addGap(18, 18, 18)
                .addComponent(jLabelSyncFreq)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton5Min)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton30Min)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton1Hour)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2Hour)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButtonNeverAutoSync)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOK)
                    .addComponent(Cancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // TODO add your handling code here:
        // Do setting here
        // Langguage
        FreeDoitConfig.setLocale(bgLang.getSelection().getActionCommand());
        // Synchronize Frequency
        FreeDoitConfig.setSyncFreq(
                Long.parseLong(bgSyncFreq.getSelection().getActionCommand()));

        // close dialog
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        // TODO add your handling code here:
        // close dialog
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_CancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SettingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SettingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SettingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SettingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SettingDialog dialog = new SettingDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    private javax.swing.ButtonGroup bgLang = new javax.swing.ButtonGroup();
    private javax.swing.ButtonGroup bgSyncFreq = new javax.swing.ButtonGroup();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JLabel jLabelLang;
    private javax.swing.JLabel jLabelSyncFreq;
    private javax.swing.JRadioButton jRadioButton1Hour;
    private javax.swing.JRadioButton jRadioButton2Hour;
    private javax.swing.JRadioButton jRadioButton30Min;
    private javax.swing.JRadioButton jRadioButton5Min;
    private javax.swing.JRadioButton jRadioButtonChs;
    private javax.swing.JRadioButton jRadioButtonCht;
    private javax.swing.JRadioButton jRadioButtonEn;
    private javax.swing.JRadioButton jRadioButtonNeverAutoSync;
    // End of variables declaration//GEN-END:variables
}
